package net.sf.filelink;

import java.util.ArrayList;
import java.util.List;

import net.sf.filelink.hover.FileResourceHover;
import net.sf.filelink.hyperlink.FileResourceHyperlinkDetector;

import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
import org.eclipse.wst.sse.core.text.IStructuredPartitions;
import org.eclipse.wst.xml.core.text.IXMLPartitions;
import org.eclipse.wst.xml.ui.StructuredTextViewerConfigurationXML;

public class FileLinkTextViewerConfiguration extends StructuredTextViewerConfigurationXML {

    @Override
    public IHyperlinkDetector[] getHyperlinkDetectors(ISourceViewer sourceViewer) {
        if (sourceViewer == null
                || !fPreferenceStore
                        .getBoolean(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_HYPERLINKS_ENABLED)) {
            return null;
        }

        List<IHyperlinkDetector> allDetectors = new ArrayList<IHyperlinkDetector>();
        allDetectors.add(new FileResourceHyperlinkDetector());
        IHyperlinkDetector[] superDetectors = super.getHyperlinkDetectors(sourceViewer);
        for (IHyperlinkDetector detector : superDetectors) {
            if (!allDetectors.contains(detector)) {
                allDetectors.add(detector);
            }
        }
        return allDetectors.toArray(new IHyperlinkDetector[0]);
    }

    @Override
    public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
        if ((contentType == IStructuredPartitions.DEFAULT_PARTITION) || (contentType == IXMLPartitions.XML_DEFAULT)) {
            return new FileResourceHover(sourceViewer);
        }
        return super.getTextHover(sourceViewer, contentType, stateMask);
    }
}
